home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Sherlock 2.0 / DevLibSrc / Main_DevLib / LIBmem.h < prev    next >
Text File  |  1995-11-16  |  4KB  |  151 lines

  1. /*
  2.     devlib: memory allocation functions.
  3.         
  4.     source:  LIBmem.h
  5.     started: September 22, 1993.
  6.     version:
  7.         November 14, 1995.
  8.             Added `mem_free_life_node' and `mem_free_stat_node'.
  9.             Added `lib_calloc' and `lib_free'.
  10.         May 24, 1994.
  11.             Changed mem_stat_bytes to mem_cur_stat_bytes.
  12.             Changed mem_stat_nodes to mem_cur_stat_nodes.
  13.         April 24, 1994.
  14.             Added maximum statistics.
  15.         April 6, 1994.
  16.             Define mem_life_list here, not in XXmem.h.
  17.         April 5, 1994.
  18. */
  19.  
  20. #ifndef LIBmem_h_
  21. #define LIBmem_h_
  22.  
  23. #pragma once
  24.  
  25. #include <string.h> 
  26.  
  27. /*
  28.     Enable or disable local statistics.
  29. */
  30. #undef  DO_MEM_STATS
  31. #define DO_MEM_STATS
  32.  
  33. #ifdef DO_MEM_STATS
  34.     #define MEM_STATS(s) s
  35.     #define MEM_UPDATE_STATS(life_stat,size) \
  36.         {\
  37.             ASSERT(life_stat);\
  38.             life_stat -> mem_cur_stat_nodes++;\
  39.             life_stat -> mem_cur_stat_bytes += (size);\
  40.         }
  41. #else
  42.     #define MEM_STATS(s)
  43.     #define MEM_UPDATE_STATS(life_stat,size) 
  44. #endif
  45.  
  46. /*
  47.     Define the types used by the memory manager.
  48. */
  49. typedef struct mem_life_struct    mem_life;
  50. typedef struct mem_block_struct mem_block;
  51. typedef struct mem_stat_struct    mem_stat;
  52.  
  53. /*
  54.     The following structure describes the memory with a given lifetime.
  55. */
  56. struct mem_life_struct {
  57.  
  58.         /* The name of the lifetime. */
  59.         
  60.     char *    mem_life_name;
  61.  
  62.         /* Status variables. */
  63.     
  64.     void  *    mem_ptr;    /* Pointer to the next free byte. */
  65.     long    mem_avail;    /* Number of available bytes in the current block. */
  66.     
  67.         /* Lists. */
  68.         
  69.     mem_life *    mem_life_list;        /* List of all lifetimes. */
  70.     mem_block *    mem_block_list;        /* List of allocated blocks. */        
  71.     mem_stat *    mem_life_stat_list;    /* List of all statistics nodes. */
  72.     
  73.     long mem_last_avail;    /* Available bytes in the last allocated block. */
  74.         
  75.     long mem_tot_blocks;    /* Total number of normal blocks. */
  76.     long mem_tot_nodes;        /* Total number of normal nodes. */
  77.     long mem_tot_bytes;        /* Total number of bytes in normal nodes. */
  78.     long mem_tot_waste;        /* Total number of unused bytes in normal nodes. */
  79.  
  80.     long mem_tot_big_blocks;    /* Total number of normal blocks. */
  81.     long mem_tot_big_bytes;        /* Total number of bytes in big nodes. */
  82.  
  83.     long mem_cur_blocks;    /* Current number of normal blocks. */
  84.     long mem_cur_nodes;        /* Current number of normal nodes. */
  85.     long mem_cur_bytes;        /* Current number of bytes in normal nodes. */
  86.     long mem_cur_waste;        /* Current number of unused bytes in normal nodes. */
  87.  
  88.     long mem_cur_big_blocks;    /* Current number of normal blocks. */
  89.     long mem_cur_big_bytes;        /* Current number of bytes in big nodes. */
  90.     
  91.     long mem_max_blocks;    /* Current number of normal blocks. */
  92.     long mem_max_nodes;        /* Current number of normal nodes. */
  93.     long mem_max_bytes;        /* Current number of bytes in normal nodes. */
  94.     long mem_max_waste;        /* Current number of unused bytes in normal nodes. */
  95.  
  96.     long mem_max_big_blocks;    /* Current number of normal blocks. */
  97.     long mem_max_big_bytes;        /* Current number of bytes in big nodes. */
  98. };
  99.  
  100. struct mem_stat_struct {
  101.     TYPE_LIST(mem_stat);
  102.     char * mem_stat_node_tag;
  103.  
  104.     long mem_cur_stat_nodes;    /* Current statistics. */
  105.     long mem_cur_stat_bytes;
  106.     
  107.     long mem_tot_stat_nodes;    /* Total statistics. */
  108.     long mem_tot_stat_bytes;
  109.     
  110.     long mem_max_stat_nodes;    /* Maxima statistics. */
  111.     long mem_max_stat_bytes;
  112. };
  113.  
  114. /*
  115.     Normal blocks are allocated in chunks of MEM_BLOCK_SIZE.
  116. */
  117. #define MEM_BLOCK_SIZE 1024
  118. #define MEM_ALLOC_SIZE (MEM_BLOCK_SIZE-sizeof(mem_block *))
  119.  
  120. struct mem_block_struct {
  121.     TYPE_LIST(mem_block);
  122.     char mem_data [MEM_ALLOC_SIZE];
  123. };
  124.  
  125. /*
  126.     Function prototypes.
  127. */
  128. #define DUMP_CURRENT_STATS        TRUE
  129. #define DONT_DUMP_CURRENT_STATS    FALSE
  130.  
  131. void *    lib_calloc    (size_t, size_t);
  132. void    lib_free    (void * p);
  133.  
  134. void        mem_compute_max_stats (mem_life * life);
  135. void        mem_dump_stats        (mem_life * life);
  136. void        mem_free_life        (mem_life * life);
  137. void        mem_free_life_node    (register mem_life * life);
  138. void        mem_free_stat_node    (mem_stat * the_stat);
  139. mem_life *    mem_init_life        (char * life_name);
  140. mem_stat *    mem_init_stats        (mem_life * life, char * node_tag);
  141. void *        mem_new_big_block    (size_t size, mem_life * life, char * dtag);
  142. void *        mem_new_block        (size_t size, mem_life * life);
  143. char *        mem_str_copy        (char * start, size_t length);
  144.  
  145. /*
  146.     Variables.
  147. */
  148. extern mem_life * mem_life_list;    /* List of all lifetimes. */
  149.  
  150. #endif /* LIBmem_h_ */
  151.